<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$setting = $this->plugin_setting('setting');
set_html('title','注册用户');
if($_RQ['op']=='get'){
    if($_SESSION['os']!='windows'){
        exi('这个只有PC端，请在PC页面登录！','error');
    }
	if(!$_RQ['add'] && $_SESSION['uid']){
		$item=pdo_get('jy_lvpai_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		if($item){
			if($item['status']==1){
				exi('你已申请无需重复申请!','error','jy_lvpai/manage.index/');
			}
		}
	}
	if(empty($item)){
		$member = pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','realname','credit2','mobile','groupid','avatar','nickname'));
		$item =array(
			'title'=>$member['nickname'],
			'realname'=>$member['realname'],
			'mobile'=>$member['mobile'],
			'logo'=>$member['avatar'],
		);
	}
	$return['set']=$setting;
	$item['upass']='';
	$return['cate']=pdo_getall('jy_lvpai_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	$return['item']=$item;
	$return['set']['regbg']=tomedia($return['set']['regbg']);
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['realname']){
		exi('请填写联系人','error');
	}
	if(!$postdata['mobile']){
		exi('请填写手机号','error');
	}
	if(strlen($postdata['mobile'])!=11){
		exi('请填写正确的手机号','error');
	}
	if(!$postdata['uname']){
		exi('请填写用户名','error');
	}
	if(!$postdata['check']){
		//exi('请先阅读协议！','error');
	}
	$data =array(
		'uname'=>$postdata['uname'],
		'mobile'=>$postdata['mobile'],
		'realname'=>$postdata['realname'],
		'qq'=>$postdata['qq'],
		'email'=>$postdata['email'],
		'sex'=>$postdata['sex'],
		'status'=>$setting['isshenhe']?0:1,		
	);
	if($postdata['upass']){
		$data['upass']=base64_encode($postdata['upass']);
	}
	if($postdata['id']){
		pdo_update('jy_lvpai_member',$data,array('id'=>$postdata['id'],'mid'=>$_SESSION['uid']));
	}else{
		$log=pdo_get('jy_lvpai_member',array('uniacid'=>$_SESSION['uniacid'],'uname'=>$postdata['uname']));
		if($log){
			exi('用户名已被注册','error');
		}
		$data['uniacid']=$_SESSION['uniacid'];
		$data['mid']=$_SESSION['uid'];
		$data['createtime']=time();
		pdo_insert('jy_lvpai_member',$data);
		$memberid=pdo_insertid();
		do{
			$username='user_'.random(10);
		}while(pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$username),'id'));
		$member=array('username'=>$username,'password'=>trim($user['upass']));
		$uid=model('core/member')->register($member);
		pdo_update('jy_lvpai_member',array('mid'=>$uid),array('id'=>$memberid));
	}
	if($setting['isshenhe']){
		exi('已申请,请等待审核','','jy_lvpai/index/');
	}else{
		exi('注册成功，请登录！','','jy_lvpai/login/');
	}
}
?>